/**
 * Created by zhangb on 2015/10/18.
 */

/**
 * 无论打算在页面加载完毕时执行多少个函数，只需要多谢一条语句就可以完成
 * @param func 打算在页面加载完毕时执行的函数的名字
 */
function addLoadEvent(func)
{
    var oldonload = window.load;
    if(typeof window.onload != 'function')
    {
        window.onload = func;
    }else{
        window.onload = function(){
            oldonload();
            func();
        }
    }
}

/**
 * 创建xmlhttprequest
 * @returns {XMLHttpRequest}
 */
function getHTTPObject()
{
    if(typeof XMLHttpRequest == "undefined")
        XMLHttpRequest = function()
        {
            try{
                return new ActiveXObject("Msxml2.XMLHTTP.6.0");
            }catch (e){}
            try{
                return new ActiveXObject("Msxml2.XMLHTTP.3.0");
            }catch (e){}
            try{
                return new ActiveXObject("Mxxml2.XMLHTTP");
            }catch (e){}
            return false;
        };
    return new XMLHttpRequest();
}

/**
 *
 */
function getNewContent()
{
    var request = getHTTPObject();
    console.log(request);
    if(request)
    {
        request.open("GET","exam.txt",true);
        request.onreadystatechange = function()
        {
            if(request.readyState == 4)
            {
                alert('Response Received');
                var para = document.createElement('p');
                var txt = document.createTextNode(request.responseText);
                para.appendChild(txt);
                document.getElementById('new').appendChild(para);
            }
        };
        request.send(null);
    }else{
        alert('Sorry,your browser doesn\'t support XMLHttpRequest');
    }
    alert('Function Done');
}
